home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / object oriented files / GNUStaticPane.cp < prev    next >
Encoding:
Text File  |  1993-11-29  |  11.7 KB  |  556 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  GNUStaticPane.c
  3.  
  4. A superclass of CAbstractText, with functionality similar to CEditPane in static
  5. text mode, but does
  6. not make use of textedit, so can handle arbitrary size buffers.
  7.  
  8. Body of Draw, CalcLineStarts, IndexToLine methods 
  9. Incorporate code originally written by
  10. Roy Wood 122 Britannia Avenue London, Ontario, Canada N6H 2J5
  11.  
  12. Brazenly hacked for TCL compatibility by Jonathan Kimmitt.
  13.  
  14. The many bugs introduced during this process are entirely my fault
  15.  
  16. */
  17.  
  18. #include <CDocument.h>
  19. #include "GNUStaticPane.h"
  20. #include "CClipboard.h"
  21. #include "Commands.h"
  22. #include "TCLUtilities.h"
  23. #include "Constants.h"
  24. #include "CScrollPane.h"
  25. #include "Global.h"
  26.  
  27. extern CClipboard *gClipboard;
  28. extern EventRecord gLastMouseUp;
  29. extern CBureaucrat *gGopher;
  30.  
  31. void GNUStaticPane::IGNUStaticPane(CView *anEnclosure, CBureaucrat *aSupervisor)
  32. {
  33.     Str255          itemStr;
  34.     FontInfo        macFontInfo;
  35.     Rect    margin;
  36.     Boolean         savedAlloc;
  37.     
  38.     CAbstractText::IAbstractText(anEnclosure, aSupervisor,
  39.             1, 1, 0, 0, sizELASTIC, sizELASTIC,
  40.                      432);
  41.     wholeLines = 1;
  42.  
  43.     UseLongCoordinates(1);
  44.     SetPort(macPort);
  45.  
  46.     savedAlloc = SetAllocation(kAllocCanFail);
  47.     lineStarts = (long **) NewHandle(sizeof(long) * 1000);
  48.     hText = NewHandle(0);
  49.     frame_origin.h = frame.left;
  50.     frame_origin.v = frame.top;
  51.     teLength = 0L;
  52.     txFont = monaco;
  53.     txFace = 0;
  54.     txMode = srcCopy;
  55.     txSize = 9;
  56.     tabWidth = 25;
  57.     nLines = 1L;
  58.     (*lineStarts)[0] = 0L;
  59.     (*lineStarts)[1] = 0L;
  60.     SetAllocation(savedAlloc);
  61.     FailNIL((Handle)lineStarts);
  62.     SetWholeLines(wholeLines);
  63.     SetFontStuff();
  64.     AdjustBounds();
  65.     spacingCmd = 50L;
  66.     alignCmd = 41L;
  67.     
  68.     FitToEnclosure(TRUE, TRUE);
  69.  
  70.     SetRect(&margin, 2, 2, -2, -2);
  71.     ChangeSize(&margin, FALSE);
  72. }
  73.  
  74. long GNUStaticPane::indexToLine(long selIndex)
  75. {
  76.     register long   i, delta;
  77.  
  78.         if (selIndex <= 0L || nLines <= 1L || teLength < 1L)
  79.             return (0L);
  80.         else if (selIndex >= teLength)
  81.             return ((long) (nLines - 1L));
  82.         else
  83.         {
  84.             i = (nLines) >> 1;
  85.             delta = (nLines) >> 1;
  86.             if (delta < 1L)
  87.                 delta = 1L;
  88.             while (delta > 0L)
  89.             {
  90.                 if (selIndex == (*lineStarts)[i])
  91.                     delta = 0L;
  92.                 else if (selIndex > (*lineStarts)[i])
  93.                 {
  94.                     if (selIndex < (*lineStarts)[i + 1])
  95.                         delta = 0L;
  96.                     else
  97.                         i += delta;
  98.                 } else
  99.                     i -= delta;
  100.                 if (delta)
  101.                 {
  102.                     delta >>= 1;
  103.                     if (delta < 1L)
  104.                         delta = 1L;
  105.                 }
  106.             }
  107.         }
  108.         if (i < 0L)
  109.             i = 0L;
  110.         else if (i >= nLines)
  111.             i = nLines - 1L;
  112.         return ((long) i);
  113. }
  114.  
  115. void            GNUStaticPane::SetFontStuff(void)
  116. {
  117.     register long  i;
  118.     long           oldFont, oldFace, oldSize, oldMode;
  119.     GrafPtr         oldPort;
  120.     FontInfo        theFontInfo;
  121.     short            tot = 0,totwidth = 0;
  122.  
  123.     GetPort(&oldPort);
  124.     SetPort(thePort);
  125.     oldFont = (thePort)->txFont;
  126.     oldFace = (thePort)->txFace;
  127.     oldSize = (thePort)->txSize;
  128.     oldMode = (thePort)->txMode;
  129.     TextFont(txFont);
  130.     TextFace(txFace);
  131.     TextSize(txSize);
  132.     TextMode(txMode);
  133.     for (i = 0; i < 256; i++)
  134.         {
  135.         short width = CharWidth((unsigned char) i);
  136.         theCharWidths[i] = width;
  137.         if (width)
  138.             {
  139.             totwidth += width;
  140.             tot++;
  141.             }
  142.         }
  143.     GetFontInfo(&theFontInfo);
  144.     tabWidth = totwidth*4/tot;
  145.     lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  146.     SetScales(theFontInfo.widMax, lineHeight);
  147.     fontAscent = theFontInfo.ascent;
  148.     TextFont(oldFont);
  149.     TextFace(oldFace);
  150.     TextSize(oldSize);
  151.     TextMode(oldMode);
  152.     SetPort(oldPort);
  153.     }
  154.     
  155. void            GNUStaticPane::CalcLineStarts(void)
  156.     { 
  157.     register unsigned char *charPtr;
  158.     register long   charCount;
  159.     register short     lineLength,maxLineLength;
  160.     register unsigned char ch;
  161.     long            maxLineStarts;
  162.     unsigned char  *charBase;
  163.     unsigned char  *oldCharPtr;
  164.     long            oldCharCount, tempOffset;
  165.     (*lineStarts)[0] = 0L;
  166.     (*lineStarts)[1] = 0L;
  167.     maxLineStarts = GetHandleSize((Handle) lineStarts) / (long) sizeof(long) - 2;
  168.     lineLength = 0L;
  169.     maxLineLength = 0;
  170.     nLines = 0L;
  171.     charBase = (unsigned char *) *(hText);
  172.     charPtr = charBase;
  173.     charCount = teLength;
  174.     if (charCount > 0L)
  175.     {
  176.         while (charCount--)
  177.         {
  178.             ch = *charPtr++;
  179.             lineLength++;
  180.             if ((ch == '\r' || ch == '\n'))
  181.             {
  182.                 if (nLines >= maxLineStarts)
  183.                 {
  184.                     tempOffset = charPtr - charBase;
  185.                     maxLineStarts = nLines+1000;
  186.                     SetHandleSize((Handle) lineStarts, sizeof(long) * (maxLineStarts+2));
  187.                     charBase = (unsigned char *) *(hText);
  188.                     charPtr = charBase + tempOffset;
  189.                 }
  190.                 (*lineStarts)[++nLines] = charPtr - charBase;
  191.                 if (lineLength > maxLineLength) maxLineLength = lineLength;
  192.                 lineLength = 0L;
  193.             }
  194.         }
  195.         SetHandleSize((Handle) lineStarts, (long) sizeof(long) * (nLines + 3));
  196.         (*lineStarts)[++nLines] = charPtr - charBase;
  197.     }
  198. }
  199.  
  200. void            GNUStaticPane::Dispose(void)
  201.     {
  202.     if (hText)
  203.         DisposHandle(hText);
  204.     if (lineStarts)
  205.         DisposHandle((Handle)lineStarts);
  206.     inherited::Dispose();
  207.     }
  208.  
  209. void            GNUStaticPane::Draw(Rect * area)
  210. {
  211.     GrafPtr         oldPort;
  212.     register unsigned char *textPtr;
  213.     register long   firstLine, i, thisStart, nextStart;
  214.     Point           cursorPt;
  215.     short           oldFont, oldFace, oldSize, oldMode, height, gap;
  216.     LongRect        longInterior;
  217.     Rect            all;
  218.     
  219.     GetInterior(&longInterior);
  220.     FrameToQDR(&longInterior, &all);
  221.  
  222.     GetPort(&oldPort);
  223.     SetPort(thePort);
  224.     firstLine = position.v+(area->top-all.top)/lineHeight;
  225.     if (firstLine < 0) firstLine = 0;
  226.     if (firstLine < nLines && teLength > 0L)
  227.         {
  228.         cursorPt.h = all.left;
  229.         cursorPt.v = all.top+(firstLine-position.v+1)*lineHeight;
  230.         oldFont = (thePort)->txFont;
  231.         oldFace = (thePort)->txFace;
  232.         oldSize = (thePort)->txSize;
  233.         oldMode = (thePort)->txMode;
  234.         TextFont(txFont);
  235.         TextFace(txFace);
  236.         TextSize(txSize);
  237.         TextMode(txMode);
  238.         HLock(hText);
  239.         textPtr = (unsigned char *) *(hText);
  240.         while (firstLine < nLines && cursorPt.v <= area->bottom)
  241.         {
  242.             thisStart = (*lineStarts)[firstLine];
  243.             i = thisStart;
  244.             nextStart = (*lineStarts)[firstLine + 1];
  245.             if (nextStart > thisStart && (textPtr[nextStart - 1] == '\r' || textPtr[nextStart - 1] == '\n'))
  246.                 nextStart--;
  247.             MoveTo(cursorPt.h, cursorPt.v);
  248.             while (thisStart < nextStart)
  249.             {
  250.                 while (i < nextStart && textPtr[i] != '\t')
  251.                     i++;
  252.                 if (i > thisStart)
  253.                     DrawText(&(textPtr[thisStart]), 0, (long) (i - thisStart));
  254.                 if (i < nextStart && textPtr[i] == '\t')
  255.                 {
  256.                     MoveTo(area->left + ((thePort->pnLoc.h - area->left + tabWidth) / tabWidth) * tabWidth, thePort->pnLoc.v);
  257.                     i++;
  258.                 }
  259.                 thisStart = i;
  260.                 if (thePort->pnLoc.h > area->right)
  261.                     thisStart = nextStart;
  262.             }
  263.             firstLine++;
  264.             cursorPt.v += lineHeight;
  265.         }
  266.         HUnlock(hText);
  267.         TextFont(oldFont);
  268.         TextFace(oldFace);
  269.         TextSize(oldSize);
  270.         TextMode(oldMode);
  271.         }
  272.     SetPort(oldPort);
  273.     ((CDocument *)itsSupervisor)->dirty = 0;
  274. }
  275.  
  276. void            GNUStaticPane::GetSteps(short *hStep, short *vStep)
  277. {
  278.     short           phStep, pvStep;
  279.  
  280. CPanorama::GetSteps(&phStep, &pvStep);
  281.     *hStep = phStep;
  282.     *vStep = pvStep;
  283. }
  284.  
  285. void            GNUStaticPane::Scroll(
  286.        long hDelta,
  287.        long vDelta,
  288.        Boolean redraw)
  289. {
  290.     inherited::Scroll(hDelta, vDelta, redraw);
  291. }
  292.  
  293. void            GNUStaticPane::PrintPage(
  294.       short pageNum,
  295.       short pageWidth,
  296.       short pageHeight,
  297.       CPrinter * aPrinter)
  298. {
  299.     inherited::PrintPage(pageNum, pageWidth, pageHeight, aPrinter);
  300. }
  301.  
  302. void            GNUStaticPane::SetTextPtr(
  303.        Ptr textPtr,
  304.        long textLength)
  305. {
  306.         SetHandleSize(hText, textLength);
  307.         BlockMove(textPtr, *hText, textLength);
  308.         teLength = textLength;
  309.         CalcLineStarts();
  310.         SetFontStuff();
  311.     AdjustBounds();
  312.     Refresh();
  313. }
  314.  
  315. Handle          GNUStaticPane::GetTextHandle(void)
  316. {
  317.     return hText;
  318. }
  319.  
  320. void            GNUStaticPane::SetFontNumber(short aFontNumber)
  321. {
  322.     txFont = aFontNumber;
  323.     SetFontStuff();
  324.     SetSpacingCmd(spacingCmd);
  325.     AdjustBounds();
  326.     Refresh();
  327.     RefreshBorder();
  328.     SetWholeLines(wholeLines);
  329.     Refresh();
  330.     RefreshBorder();
  331. }
  332.  
  333. void            GNUStaticPane::SetFontStyle(short aStyle)
  334. {
  335.     if (aStyle == 0)
  336.     {
  337.         txFace = 0;
  338.     } else
  339.     {
  340.         txFace ^= aStyle;
  341.     }
  342.     SetFontStuff();
  343.     Refresh();
  344.     RefreshBorder();
  345.     SetWholeLines(wholeLines);
  346.     Refresh();
  347.     RefreshBorder();
  348. }
  349.  
  350. void            GNUStaticPane::SetFontSize(short aSize)
  351. {
  352.     txSize = aSize;
  353.     SetFontStuff();
  354.     SetSpacingCmd(spacingCmd);
  355. }
  356.  
  357. void            GNUStaticPane::SetTextMode(short aMode)
  358. {
  359.     txMode = aMode;
  360.     SetFontStuff();
  361.     Refresh();
  362. }
  363.  
  364. void            GNUStaticPane::SetSpacingCmd(long aSpacingCmd)
  365. {
  366.     FontInfo        macFontInfo;
  367.     long           extra;
  368.  
  369.     spacingCmd = aSpacingCmd;
  370.     SetFontStuff();
  371.     switch (aSpacingCmd)
  372.     {
  373.     case 50L:
  374.         extra = 0;
  375.         break;
  376.     case 51L:
  377.         extra = lineHeight / 2;
  378.         break;
  379.     case 52L:
  380.         extra = lineHeight;
  381.         break;
  382.     }
  383.     lineHeight += extra;
  384.     fontAscent += extra;
  385.     Refresh();
  386.     RefreshBorder();
  387.     SetWholeLines(wholeLines);
  388.     CalcAperture();
  389.     AdjustBounds();
  390.     Refresh();
  391.     RefreshBorder();
  392. }
  393.  
  394. long            GNUStaticPane::GetSpacingCmd(void)
  395. {
  396.     return spacingCmd;
  397. }
  398.  
  399. long            GNUStaticPane::GetAlignCmd(void)
  400. {
  401.     return alignCmd;
  402. }
  403.  
  404. void            GNUStaticPane::GetGNUTEFontInfo(
  405.          FontInfo * macFontInfo)
  406. {
  407.     SetPort(macPort);
  408.     ForceNextPrepare();
  409.     TextFont(txFont);
  410.     TextFace(txFace);
  411.     TextSize(txSize);
  412.     GetFontInfo(macFontInfo);
  413. }
  414.  
  415. void            GNUStaticPane::ResizeFrame(
  416.         Rect * delta)
  417. {
  418.     short           hDelta;
  419.     short           vDelta;
  420.     inherited::ResizeFrame(delta);
  421.  
  422.     frame_origin.h += delta->left;
  423.     frame_origin.v += delta->top;
  424.  
  425.     hDelta = position.h * hScale - frame.left + frame_origin.h;
  426.     vDelta = position.v * vScale - frame.top + frame_origin.v;
  427.     frame.left += hDelta;
  428.     frame.right += hDelta;
  429.     frame.top += vDelta;
  430.     hOrigin += hDelta;
  431.     vOrigin += vDelta;
  432.     SetFontStuff();
  433.     AdjustBounds();
  434.     Refresh();
  435. }
  436.  
  437. void            GNUStaticPane::AdjustBounds(void)
  438. {
  439.     bounds.left = bounds.top = 0;
  440.     bounds.bottom = GetHeight(0, nLines);
  441.     bounds.right = (lineWidth - 1) / hScale + 1;
  442.     if (itsScrollPane != ((void *) 0))
  443.     {
  444.         itsScrollPane->AdjustScrollMax();
  445.     }
  446. }
  447.  
  448. long            GNUStaticPane::FindLine(
  449.      long charPos)
  450. {
  451.     long            lineNum;
  452.  
  453.     if ((nLines == 0) || (charPos < (*lineStarts)[1]))
  454.     {
  455.         return (0);
  456.     }
  457.     if (charPos >= (*lineStarts)[nLines - 1])
  458.         if (charPos == teLength)
  459.             if (((char *) (*hText))[charPos - 1] == 13)
  460.                 return nLines;
  461.             else
  462.                 return nLines - 1;
  463.     lineNum = 1;
  464.     while (charPos >= (*lineStarts)[lineNum + 1])
  465.     {
  466.         lineNum++;
  467.     }
  468.     return (lineNum);
  469. }
  470.  
  471. long            GNUStaticPane::GetLength(void)
  472. {
  473.     return teLength;
  474. }
  475.  
  476. void            GNUStaticPane::GetTextStyle(short *whichAttributes, TextStyle * aStyle)
  477. {
  478.     aStyle->tsFont = txFont;
  479.     aStyle->tsFace = txFace;
  480.     aStyle->tsSize = txSize;
  481.     aStyle->tsColor.red = 1;
  482.     aStyle->tsColor.green = 1;
  483.     aStyle->tsColor.blue = 1;
  484. }
  485.  
  486. void            GNUStaticPane::GetCharStyle(long charOffset, TextStyle * aStyle)
  487. {
  488.     aStyle->tsFont = txFont;
  489.     aStyle->tsFace = txFace;
  490.     aStyle->tsSize = txSize;
  491.     aStyle->tsColor.red = 1;
  492.     aStyle->tsColor.green = 1;
  493.     aStyle->tsColor.blue = 1;
  494. }
  495.  
  496. void            GNUStaticPane::GetExtent(
  497.       long *theHExtent,
  498.       long *theVExtent)
  499. {
  500.     LongRect        longInterior;
  501.  
  502.     GetInterior(&longInterior);
  503.     *theVExtent = nLines;
  504.     *theHExtent = longInterior.right - longInterior.left;
  505. }
  506.  
  507. long            GNUStaticPane::GetHeight(long startLine, long endLine)
  508. {
  509.     long            height;
  510.  
  511.     if (endLine > nLines)
  512.     {
  513.         height = nLines - startLine + 1;
  514.         if (((char *) *hText)[teLength - 1] == 13)
  515.             ++height;
  516.     } else
  517.         height = endLine - startLine + 1;
  518.     return height * lineHeight;
  519. }
  520.  
  521. void            GNUStaticPane::GetSelection(long *selStart, long *selEnd)
  522. {
  523.     *selStart = 0;
  524.     *selEnd = 0;
  525. }
  526.  
  527. long GNUStaticPane::GetCharOffset( LongPt *aPt)
  528. {
  529.     return 0L;
  530. }
  531.  
  532. void GNUStaticPane::GetCharPoint( long offset, LongPt *aPt)
  533. {
  534. }
  535.  
  536. void GNUStaticPane::TypeChar(char theChar, short theModifers)
  537. {
  538. }    
  539.  
  540. void GNUStaticPane::SetSelection( long selStart, long selEnd, Boolean fRedraw)
  541. {
  542. }    
  543.  
  544. Handle GNUStaticPane::CopyTextRange( long start, long end)
  545. {
  546.     return NULL;
  547. }
  548.  
  549. void GNUStaticPane::PerformEditCommand( long theCommand)
  550. {
  551. }
  552.  
  553. void GNUStaticPane::InsertTextPtr( Ptr text, long length, Boolean fRedraw)
  554. {
  555. }
  556.